package com.hlju.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlju.reggie.bean.Category;
import com.hlju.reggie.bean.Setmeal;
import com.hlju.reggie.bean.SetmealDish;
import com.hlju.reggie.bean.dto.SetmealDto;
import com.hlju.reggie.common.CustException;
import com.hlju.reggie.common.R;
import com.hlju.reggie.service.CategoryService;
import com.hlju.reggie.service.SetmealDishService;
import com.hlju.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    //分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize,String name) {
        //创建分页对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        //创建条件构造器
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        if(name !=null && !name.equals("")){
            wrapper.like("name",name);
        }
        //执行分页
        setmealService.page(pageInfo,wrapper);

        //创建dto的Page对象
        Page<SetmealDto> dtoPage = new Page<>();
        //属性复制(排除数据集合)
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        //获取pageinfo中的数据集合
        List<Setmeal> setmealList = pageInfo.getRecords();
        //新建一个dto的集合
        List<SetmealDto> dtoList = new ArrayList<>();
        //遍历套餐集合
        for (Setmeal setmeal : setmealList) {
            //创建dto对象
            SetmealDto dto = new SetmealDto();
            //属性复制
            BeanUtils.copyProperties(setmeal,dto);
            //根据套餐对象中的分类id 查询分类对象
            Category category = categoryService.getById(setmeal.getCategoryId());
            if(category != null){
                //给dto设置分类名称
                dto.setCategoryName(category.getName());
            }
            //将dto添加进集合
            dtoList.add(dto);
        }
        //给dto的集合设置值
        dtoPage.setRecords(dtoList);
        //返回结果
        return R.sucess(dtoPage);
    }

    //新增套餐
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> saveSetmealWithDish(@RequestBody SetmealDto dto){
        setmealService.saveSetmealWithDish(dto);
        return R.sucess("新增成功");
    }

    //根据ID获取套餐
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable("id") Long id){
        return R.sucess( setmealService.getSetmealWithDish(id));
    }

    //修改
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateSetmealWithDish(@RequestBody SetmealDto dto){
        setmealService.updateSetmealWithDish(dto);
        return R.sucess("修改套餐成功");
    }

    //修改状态
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> status(@PathVariable("status") Integer status,Long[] ids){
        List<Setmeal> setmealList = new ArrayList<>();
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealList.add(setmeal);
        }
        return setmealService.updateBatchById(setmealList)?R.sucess("修改成功"):R.error("修改失败");
    }

    //删除套餐
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(Long[] ids) throws CustException {
        setmealService.deleteSetmealWithDish(ids);
        return R.sucess("删除成功");
    }

    //根据分类id查询套餐
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_'+#setmeal.status")
    @GetMapping("/list")
    public R<List> list(Setmeal setmeal){
        //根据分类id查询套餐
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",setmeal.getCategoryId());
        wrapper.eq("status",setmeal.getStatus());
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.sucess(setmealList);
    }

    //根据套餐id查询关联的菜品关系数据
    @GetMapping("/dish/{id}")
    public R<List> getDishListById(@PathVariable("id") Long id){
        //根据套餐id查询关联的菜品关系数据
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",id);
        List<SetmealDish> setmealDishList = setmealDishService.list(wrapper);
        return R.sucess(setmealDishList);
    }

}
